package cn.damai.search.baseService.ipml;

import cn.damai.common.Constants;
import cn.damai.common.EmptyUtils;
import cn.damai.search.baseService.BaseImageService;
import cn.damai.search.mapper.ImageMapper;
import cn.damai.model.Image;
import cn.damai.util.DubboUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

@Component
@Service(version = DubboUtil.VRESION,group = DubboUtil.GROUP)
public class BaseImageServiceIpml implements BaseImageService {

    @Autowired
    private ImageMapper imageMapper;

    @Override
    public Image getImageById(@RequestParam  Long id){
        return imageMapper.getImageById(id);
    }

    @Override
    public List<Image> getImageListByMap(@RequestParam Map<String,Object> param){
        return imageMapper.getImageListByMap(param);
    }

    @Override
    public Integer getImageCountByMap(@RequestParam Map<String,Object> param){
        return imageMapper.getImageCountByMap(param);
    }

    @Override
    public Integer qdtxAddImage(@RequestParam Image Image){
        Image.setCreatedTime(new Date());
        return imageMapper.insertImage(Image);
    }

    @Override
    public Integer qdtxModifyImage(@RequestParam Image Image){
        Image.setUpdatedTime(new Date());
        return imageMapper.updateImage(Image);
    }

    @Override
    public List<Image> queryImageList(@RequestParam Long targetId,
                                          @RequestParam Integer type,
                                          @RequestParam Integer category)  {
        Map<String, Object> imageParam = new HashMap<String, Object>();
        imageParam.put("targetId", targetId);
        imageParam.put("type", type);
        imageParam.put("category", category);
        List<Image> ImageList = imageMapper.getImageListByMap(imageParam);
        return setDefaultImageList(ImageList, category, type);
    }

    /**
     * 设置图片为空时默认显示图片
     * */
    @Override
    public List<Image> setDefaultImageList(List<Image> ImageList, Integer category, Integer type) {
        if (EmptyUtils.isEmpty(ImageList)) {
            ImageList = new ArrayList<Image>();
            Image Image = new Image();
            Image.setType(type);
            Image.setCategory(category);
            ImageList.add(Image);
        }
        for(Image Image:ImageList){
            if (EmptyUtils.isEmpty(Image.getImgUrl())) {
                Image.setImgUrl(Constants.DEFAULT_CAROUSEL);
            }
        }
        return ImageList;
    }
}
